package com.atguigu.gulimall.product.bn.controller;

import com.atguigu.gulimall.product.bn.bean.DataToken;
import com.atguigu.gulimall.product.bn.myuntil.AESUtil1;
import com.google.gson.Gson;
import okhttp3.*;
import org.apache.commons.codec.digest.DigestUtils;


/**
 * @author wjj
 * @create 2023-04-23 22:43
 */
public class GetPost {
    public static void main(String[] args) throws Exception {
        GetPost getPost = new GetPost();
        String bb="tOntBulT3xWPw%252BdbbT5ZJ43QzMJP4ZBmV9jQmiH%252BbgUpQ1v9CXQTml34%252BT1PX1gd6nDqIp6%252BsNQN0c9dSruVood2DUd16aUL3rw086jzdM3F1cCCD%252FMGOaRsm99kDgjFbeNitpyt1xJjg0K1ZuU%252BJjWc4Cb4WuRKg%252BdJen5nQpk%253D";
        String aa="tOntBulT3xWPw%2BdbbT5ZJ43QzMJP4ZBmV9jQmiH%2BbgUpQ1v9CXQTml34%2BT1PX1gd6nDqIp6%2BsNQN0c9dSruVood2DUd16aUL3rw086jzdM0kYVCQxayx%2FYmey0mD3j%2FMKhWMq5SMGbFRpGJmlSN%2B8oHZZCVy7g%2FwAONQ%2FR2ruMs";

        String s = getPost.post2(aa, "Lyd8d3SoDPPh1v7J8Aeox5jJE90GiV1K");
        System.out.println(s);



    }
    public  String getTong(String httpUrl) throws Exception {
        String url = httpUrl;
        OkHttpClient client = new OkHttpClient();
//        MediaType type = MediaType.parse("application/x-www-form-urlencoded");
//        RequestBody body= RequestBody.create(type, "");
        Request request = new Request.Builder()

                .header("nonce","1234")
                .header("timestamp","1234")
                .header("signature","831fe93ee640c8e27625a5b77654549b")
                .url(url)
                .get()//默认就是GET请求，可以不写

                .build();
        MyColor.println("请求头\n"+request.headers());
        Response response = client.newCall(request).execute();
//        MyColor.println(response.code());
        MyColor.println("响应头\n"+response.headers());
//        MyColor.println(response.body());
        String string = response.body().string();
        MyColor.println("响应结果\n"+test.unicodeDecode(string));
        Gson gson = new Gson();
        DataToken token=gson.fromJson(string, DataToken.class);
        String access_token = token.getAccess_token();

        return access_token;
    }
    public  String get(String httpUrl,String corpid,String appid) throws Exception {
        String url = httpUrl+"?corpid="+corpid+"&appid="+appid;
        OkHttpClient client = new OkHttpClient();
//        MediaType type = MediaType.parse("application/x-www-form-urlencoded");
//        RequestBody body= RequestBody.create(type, "");
        Request request = new Request.Builder()
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        Response response = client.newCall(request).execute();
//        MyColor.println(response.code());
//        MyColor.println(response.headers());
//        MyColor.println(response.body());
        String string = response.body().string();
        MyColor.println(string);
        Gson gson = new Gson();
        DataToken token=gson.fromJson(string, DataToken.class);
        String access_token = token.getAccess_token();

        return access_token;
    }
    public String postTong(String par,String posturl) throws Exception {
        String url = posturl;
        MyColor.println("发消息:  \n");
        MyColor.println("请求地址:  \n"+url);
        MyColor.println("请求体： \n"+par);
        OkHttpClient client = new OkHttpClient();
        MediaType type = MediaType.parse("application/json");
//        MediaType type = MediaType.parse("\"application/x-www-form-urlencoded\",");
        okhttp3.RequestBody body= RequestBody.create(type, par);

//        MyColor.println(body);
        Request request = new Request.Builder()
                .header("nonce","1234")
                .header("timestamp","1234")
                .url(url)
                .post(body)//默认就是GET请求，可以不写
                .build();
        Response response = client.newCall(request).execute();
//        MyColor.println(response.code());
//        MyColor.println(response.headers());
        String data=response.body().string();
        MyColor.println("未转响应内容：\n" + data);
        MyColor.println("响应内容：\n" + test.unicodeDecode(data));
        return data;

    }
    public String post(String par,String posturl,String token,String qianMing) throws Exception {

        String url = posturl+"?access_token="+token;
        MyColor.println("发消息:  \n");
        MyColor.println("请求地址:  \n"+url);
        MyColor.println("请求体： \n"+par);
        OkHttpClient client = new OkHttpClient();
        MediaType type = MediaType.parse("application/json");
//        MediaType type = MediaType.parse("\"application/x-www-form-urlencoded\",");
        okhttp3.RequestBody body= RequestBody.create(type, par);

//        MyColor.println(body);
        Request request = new Request.Builder()
                .header("nonce","1234")
                .header("timestamp","1234")
                .header("signature",qianMing)
                .url(url)
                .post(body)//默认就是GET请求，可以不写
                .build();
        Response response = client.newCall(request).execute();
//        MyColor.println(response.code());
//        MyColor.println(response.headers());
        String data=response.body().string();

        MyColor.println("响应内容：\n" + test.unicodeDecode(data));
        return data;

    }
    public String post2(String par,String ast) throws Exception {
        if (ast.length() >32) {
            return "App Secrect大于32位";
        }
        String s = DigestUtils.md5Hex(ast);
        String encrypt = AESUtil1.decrypt(ast, s.substring(0, 16), par);

        return encrypt;


    }
}
